<?php
/**
 * Created by coder meng.
 * User: coder meng
 * Date: 2018/4/9 13:46
 * 单品
 */
namespace app\blog\controller;
use cmf\controller\HomeBaseController;
use app\blog\model\MenuModel as ME;
use think\Db;
class MenuController extends HomeBaseController
{
    /*
     * 单品菜单选择
     * 完善商品的价格有规格表来确认
     * */
    public function menu()
    {
        $uinfo=cmf_get_current_user();
        //dump($uinfo);
        $shop_id=$uinfo['store_id'];
        $meun=new ME();
        $dat=$this->request->param();
        $where=array();
        if(!empty($dat['id']))
        {
            $where['id']=$dat['id'];
        }
        $typology=$meun->index();//总类型 不受选择的影响
        $goods=$meun->goods($shop_id,$where);
        $data=array();
        /*
         * 把商品规格和商品结合起来成为一个单品在前端进行展示
         * */
        if(!empty($goods['data'])){
            foreach($goods['data'] as $kg=>$vg)
            {
                $array['pid']=$vg['g_id'];
                $array['store_type']=$goods['store_type'];
                $format=$meun->format($array);
                foreach($format as $fk=>$fv)
                {
                    $vg['g_format']=$fv['id'];
                    $vg['g_price']=$fv['price'];
                    $vg['g_vipprice']=$fv['vipprice'];
                    $vg['g_formatname']=$fv['format'];
                    $vg['stemeal']=0;
                    $data[] = $vg;
                }
            }
        }
        if(!empty($dat['orderid']))
        {
            $where['orderid']=$dat['orderid'];
            $array=$meun->show($where);
        }
       
        $this->assign('setmeal',0);
        $this->assign('data',$data);
        $this->assign('category',$typology);
        return $this->fetch('shop/menu');
    }
    /*
     * 单种类别查询
     * 套餐
     * */
    public function show()
    {
        $menu=new ME();
        $id=$_POST['id'];
        $shop_id=50;
        $where['id']=$id;
        if($id=='taocan')
        {
            $cause['set_shopid']=$shop_id;
            $data=$menu->set($cause);
        }else{
            $goods=$menu->goods($shop_id,$where);
            $data=array();
            if(!empty($goods['data']))
            {
                foreach($goods['data'] as $kg=>$vg)
                {
                    $array['pid']=$vg['g_id'];
                    $array['store_type']=$goods['store_type'];
                    $format=$menu->format($array);
                    foreach($format as $fk=>$fv)
                    {
                        $vg['g_format']=$fv['id'];
                        $vg['g_price']=$fv['price'];
                        $vg['g_vipprice']=$fv['vipprice'];
                        $vg['g_formatname']=$fv['format'];
                        $vg['stemeal']=0;
                        $data[] = $vg;
                    }
                }
            }
        }
        $this->success($data);
    }
    /*
     * 添加购物
     * 2018-04-25 以商品id和规格来确定商品详情
     * */
    public function cart()
    {
        /*
         * 存储数据库方式
         * */
           $menu=new  ME();
           $id=$_POST['id'];
           $format=$_POST['format'];
           $get_orderid=$_POST['orderid'];
           $setmeal=$_POST['setmeal'];
           $shopid=50;
           if($setmeal==0) {
               $good = $menu->good($id, $format);
           }else{
               $where['set_id']=$id;
               $where['set_shopid']=$shopid;
               $good =DB::name('setmeal')->where($where)->find();
           }
           $good['setmeal']=$setmeal;
           $order=$menu->saveorder($good,$shopid,$get_orderid);
           $data=$menu->selorder($order)->toArray();
           foreach($data as $k=>$v)
           {
              $fid=$v['format'];
              $name=DB::name('format')->where(['id'=>$fid])->find();
              $format_name=$name['format'];
              $data[$k]['formatname']=$format_name;
           }
           $all=$menu->selorders($order);
           $pay['num']=count($data);
           $pay['orderid']=$all['orderNo'];
           $pay['all_money']=$all['totalMoney'];
           $date['data']=$data;
           $date['pay']=$pay;
           return $this->success($date);
    }
    /*
     * 清空购物车
     * */
    public  function  emptys()
    {
        $orderid=$_POST['orderid'];
        DB::name('order_detail')->where(['orderid'=>$orderid])->delete();
        DB::name('order')->where(['orderNo'=>$orderid])->delete();
        $state=1;
        return $this->success($state);
    }
    /*
     * 删除商品
     * 需要在数据库中删除
     * */
    public function del()
    {
      $menu=new ME();
      $keyid=$_POST['k'];
      $orderid=$_POST['orderid'];
      $data=$menu->del($orderid,$keyid);
      $set=$menu->setpay($data);
      $all=$menu->selorders($orderid);
      $pay['num']=count($data);
      $pay['orderid']=$all['orderNo'];
      $pay['all_money']=$all['totalMoney'];
      $array['data']=$data;
      $array['pay']=$pay;
      $this->success($array);
    }
    /*
     * 改价只是针对积分和对缓存改价
     * */
    public function change_price()
    {
       session_start();
       $data=$_SESSION['think']['goods'];
       $this->assign('data',$data);
      return $this->fetch('shop/change');
    }
    /*
     * 挂单
     * */
    public function updata()
    {
        $menu = new ME();
        $orderid=$this->request->param();
        if(!empty($orderid['orderid']))
        {
            $date['payStatus']=-1;
            $where['orderNo']=$orderid['orderid'];
            $data=$menu->updata($date,$where);
        }else{
            $where['payStatus']=-1;
            $data=DB::name('order')->where($where)->select();
        }
        $this->assign('update',$data);
        return $this->fetch('shop/update');
    }
    /*
     * 挂单详情
     * */
    public function  xq()
    {
        $orderid=$_POST['orderid'];
        $where['orderid']=$orderid;
        $order=DB::name('order_detail')->where($where)->select()->toArray();
        foreach($order as $k=>$v)
        {
            $format=$v['format'];
            $cause['id']=$format;
            $formats=DB::name('format')->where($cause)->find();
            $order[$k]['formatname']=$formats['format'];
        }
        return $this->success($order);
   }
    /*
     * 挂单选择
     * 挂单支付（未完成）
     * */
    public function xg()
    {
        $menu=new ME();
        $orderid=$_POST['orderid'];
        $where['orderid']=$orderid;
        $orders=DB::name('order_detail')->where($where)->select()->toArray();
        foreach($orders as $k=>$v)
        {
            $fid=$v['format'];
            $name=DB::name('format')->where(['id'=>$fid])->find();
            $format_name=$name['format'];
            $orders[$k]['formatname']=$format_name;
        }
        $all=$menu->selorders($orderid);
        $pay['num']=count($orders);
        $pay['orderid']=$all['orderNo'];
        $pay['all_money']=$all['totalMoney'];
        $date['data']=$orders;
        $date['pay']=$pay;
        $this->success($date);

    }

}